#include "request.hh"

#include "request-handler.hh"
#include "../tools/utility.hh"

/***
 ** GetObject
 ***/

GetObject::~GetObject ()
{
}

void
GetObject::operator() (RequestHandler* rh, const int* clientid)
{
  rh->treat (boost::dynamic_pointer_cast<GetObject> (shared_from_this ()), clientid);
}

/***
 ** AskMap
 ***/

void
AskMap::operator() (RequestHandler* rh, const int* clientid)
{
  rh->treat (boost::dynamic_pointer_cast<AskMap> (shared_from_this ()), clientid);
}

/***
 ** Connection
 ***/

Connection::~Connection ()
{
}

void
Connection::operator() (RequestHandler* rh, const int* clientid)
{
  rh->treat (boost::dynamic_pointer_cast<Connection> (shared_from_this ()), clientid);
}

/***
 ** Disconnection
 ***/

void
Disconnection::operator() (RequestHandler* rh, const int* clientid)
{
  std::cout << "trying to disconnect" << std::endl;
  rh->treat (boost::dynamic_pointer_cast<Disconnection> (shared_from_this ()), clientid);
}

/***
 ** GetAllObjects
 ***/

void
GetAllObjects::operator() (RequestHandler* rh, const int* clientid)
{
  rh->treat (boost::dynamic_pointer_cast<GetAllObjects> (shared_from_this ()), clientid);
}

/***
 ** Text
 ***/

Text::Text (const std::string& data):
  text_ (data)
{
}

void
Text::operator() (RequestHandler* rh, const int* clientid)
{
  rh->treat (boost::dynamic_pointer_cast<Text> (shared_from_this ()), clientid);
}

/***
 ** UpdateObj
 ***/

UpdateObj::UpdateObj (const std::string& data)
{
  const char* buf = data.data ();

  objid_ = Utility::from_oct (buf);
  buf += 4;
  rot_.fromnet (buf);
  buf += 12;
  newpos_.fromnet (buf);
}

UpdateObj::UpdateObj (int objid):
  objid_ (objid)
{
}

void
UpdateObj::operator() (RequestHandler* rh, const int* clientid)
{
  rh->treat (boost::dynamic_pointer_cast<UpdateObj> (shared_from_this ()), clientid);
}

/***
 ** UpdatePos
 ***/

UpdatePos::UpdatePos (const std::string& data)
{
  pos_x_ = data[0];
  pos_y_ = data[1];
}

void
UpdatePos::operator() (RequestHandler* rh, const int* clientid)
{
  rh->treat (boost::dynamic_pointer_cast<UpdatePos> (shared_from_this ()), clientid);
}

/***
 ** GameMode
 ***/

GameMode::GameMode (const std::string& data):
  mode_ (Unknown)
{
  mode_ = (GameType) data[0];
  std::cout << "mode = " << mode_;
}

void
GameMode::operator() (RequestHandler* rh, const int* clientid)
{
  rh->treat (boost::dynamic_pointer_cast<GameMode> (shared_from_this ()), clientid);
}

GameZone::GameZone (const std::string& data):
  destroy_ (true)
{
  if (!data[0])
    destroy_ = false;

  const char* buf = data.c_str ();

  ++buf;

  pos_.fromnet (buf);
  buf += 12;
  size_.fromnet (buf);
}

void
GameZone::operator() (RequestHandler* rh, const int* clientid)
{
  rh->treat (boost::dynamic_pointer_cast<GameZone> (shared_from_this ()), clientid);
}

void
Ready::operator() (RequestHandler* rh, const int* clientid)
{
  rh->treat (boost::dynamic_pointer_cast<Ready> (shared_from_this ()), clientid);
}

Move::Move (const std::string& data)
{

  std::cout << "RECEIVED : " << data << std::endl;
  const char* buf = data.c_str ();

  req_size_ = *buf;
  ++buf;
  id_ = *buf;
  ++buf;
  //size

  for (size_t i = 0; i < req_size_; ++i)
  {
    size_ = *buf;
    ++buf;
    for (size_t j = 0; j < size_; ++j)
    {
      Vector3f tmp;
      tmp.fromnet (buf);
      waypoints_.push_back (tmp);
      buf += 12;
    }
  }

}

std::list<Vector3f> Move::waypoints ()
{
  return waypoints_;
}
int Move::id ()
{
  return id_;
}

void
Move::operator() (RequestHandler* rh, const int* clientid)
{
  rh->treat (boost::dynamic_pointer_cast<Move> (shared_from_this ()), clientid);
}
